/*
**  author: wd
**  create: 2013/08/22
*/

#include "CsvDataMgr.h"
#include <iconv.h>
#include "CsvConfigReader.h"
#include <string>
#include "TLog.h"
#include "GameLogic.h"

namespace {
	static const char g_task_data_csvname[]			= "./task.csv";
	static const char g_store_data_csvname[]		= "./shangchen.csv";
	static const char g_woman_name_data_csvname[]	= "./woman_name.csv";
	static const char g_man_name_data_csvname[]		= "./man_name.csv";
	static const char g_chat_msg_data_csvname[]		= "./broadcast_chat_msg.csv";
	static const char g_gift_config_data_csvname[]	= "./gift_config.csv";
	static const char g_private_chips_csvname[]		= "./private_chaps.csv";
	static const char g_tiger_random_csvname[]		= "./tigger_config.csv";
	static const char g_pay_config_csvname[]		= "./pay_config.csv";
	static const char g_robot_bet_csvname[]			= "./robot_bet_config.csv";
}

CsvDataMgr * g_csvmgr = NULL;

CsvDataMgr::CsvDataMgr()
{
	m_is_load = false;
}
CsvDataMgr::~CsvDataMgr()
{
	ShutDown();
}

bool CsvDataMgr::Init()
{
	if (m_is_load)
		return true;

	if (!LoadStoreCSV())
		return false;
	
	if (!LoadBroadcastCharNameStringCSV())
		return false;

	if (!LoadBroadcastChatMsgCSV())
		return false;

	if (!LoadPrivateChipCsv())
		return false;

	if (!LoadTigerCsv())
		return false;

	m_is_load = true;
	return true;
}
void CsvDataMgr::ShutDown()
{
	ReleaseStoreCSV();
	ReleasePrivateChipCsv();
}

bool CsvDataMgr::LoadStoreCSV()
{
	CCsvConfigReader tmpFileReader;
	if (!tmpFileReader.OpenFile(g_store_data_csvname, false))
		return false;

	int i = 1;
	m_stores_.clear();
	for (tmpFileReader.First(i); !tmpFileReader.IsDone(); ++i, tmpFileReader.Next())	{

		int objid = tmpFileReader.GetFieldValueToInt();
		if (objid <= 0)
			continue;

		csv_store_t * tmp_obj = new csv_store_t();
		if (tmp_obj == NULL)
			return false;

		tmp_obj->index = objid;
		int tmp = tmpFileReader.GetFieldValueToInt();
		tmp_obj->golds = tmpFileReader.GetFieldValueToInt();
		tmp_obj->gems = tmpFileReader.GetFieldValueToInt();

		//WL(LL_INFO ,"index:%d,  %d, %d \n", tmp_obj->index, tmp_obj->golds, tmp_obj->gems);

		m_stores_.insert(CSV_STORE_MAP::value_type(tmp_obj->index, tmp_obj));
	}

	return true;
}
	
void CsvDataMgr::ReleaseStoreCSV()
{
	CSV_STORE_MAP::iterator it = m_stores_.begin();
	for (; it != m_stores_.end(); ++it) {
		if (it->second == NULL)
			continue;

		delete it->second;
		it->second;
	}
	m_stores_.clear();
}

bool CsvDataMgr::LoadBroadcastCharNameStringCSV()
{
	CCsvConfigReader tmpFileReader;
	if (!tmpFileReader.OpenFile(g_woman_name_data_csvname, false))
		return false;

	m_woman_names_.clear();
	int i = 0;
	for (tmpFileReader.First(i); !tmpFileReader.IsDone(); ++i, tmpFileReader.Next())	{

		std::string tmpstr = tmpFileReader.GetFieldValueToString();

		char szSource[512] = {0};
		memcpy(szSource, tmpstr.c_str(), tmpstr.length());

		char desbuff[512] = {0};
		if (gb2312toutf8(szSource, tmpstr.length(), desbuff, sizeof(desbuff)) != 0) {
			WL(LL_INFO ,"LoadBroadcastCharNameStringCSV: hang = %d,  desbuff = %s \n", i, desbuff);
			continue;
		}

		tmpstr = desbuff; 

		m_woman_names_.push_back(tmpstr);
	}

	CCsvConfigReader tmpFileReader2;
	if (!tmpFileReader2.OpenFile(g_man_name_data_csvname, false))
		return false;

	m_man_names_.clear();
	i = 0;
	for (tmpFileReader2.First(i); !tmpFileReader2.IsDone(); ++i, tmpFileReader2.Next())	{

		std::string tmpstr = tmpFileReader2.GetFieldValueToString();

		char szSource[512] = {0};
		memcpy(szSource, tmpstr.c_str(), tmpstr.length());

		char desbuff[512] = {0};
		if (gb2312toutf8(szSource, tmpstr.length(), desbuff, sizeof(desbuff)) != 0) {
			WL(LL_INFO ,"LoadBroadcastCharNameStringCSV: hang = %d,  desbuff = %s \n", i, desbuff);
			continue;
		}

		tmpstr = desbuff; 

		m_man_names_.push_back(tmpstr);
	}

	//WL(LL_INFO ,"LoadBroadcastCharNameStringCSV:  %d,  %d  \n", m_woman_names_.size(), m_man_names_.size());
	return true;
}

bool CsvDataMgr::LoadBroadcastChatMsgCSV()
{
	CCsvConfigReader tmpFileReader;
	if (!tmpFileReader.OpenFile(g_chat_msg_data_csvname, false))
		return false;

	int i = 1;
	for (tmpFileReader.First(i); !tmpFileReader.IsDone(); ++i, tmpFileReader.Next())	{

		int objid = tmpFileReader.GetFieldValueToInt();
		std::string tmpstr = tmpFileReader.GetFieldValueToString();

		char szSource[512] = {0};
		memcpy(szSource, tmpstr.c_str(), tmpstr.length());
		char desbuff[512] = {0};
		if (gb2312toutf8(szSource, tmpstr.length(), desbuff, sizeof(desbuff)) != 0) {
			WL(LL_INFO ,"LoadBroadcastChatMsgCSV: hang = %d,  desbuff = %s \n", i, desbuff);
			continue;
		}		
		
		tmpstr = desbuff; 

		int gender = tmpFileReader.GetFieldValueToInt();
		int week = tmpFileReader.GetFieldValueToInt();
		if (week == 7)
			week = 0;

		if (week < 0 || week > 7)
			continue;

		if (gender == 0) {
			m_woman_chat_msgs_[week].push_back(tmpstr);
		}
		else if (gender == 1) {
			m_man_chat_msgs_[week].push_back(tmpstr);
		}
	}

	//WL(LL_INFO ,"LoadBroadcastChatMsgCSV:  %d,  %d  \n", m_woman_chat_msgs_[0].size(), m_man_chat_msgs_[0].size());
	//WL(LL_INFO ,"LoadBroadcastChatMsgCSV:  %d,  %d  \n", m_woman_chat_msgs_[1].size(), m_man_chat_msgs_[1].size());
	//WL(LL_INFO ,"LoadBroadcastChatMsgCSV:  %d,  %d  \n", m_woman_chat_msgs_[2].size(), m_man_chat_msgs_[2].size());
	//WL(LL_INFO ,"LoadBroadcastChatMsgCSV:  %d,  %d  \n", m_woman_chat_msgs_[3].size(), m_man_chat_msgs_[3].size());
	//WL(LL_INFO ,"LoadBroadcastChatMsgCSV:  %d,  %d  \n", m_woman_chat_msgs_[4].size(), m_man_chat_msgs_[4].size());
	//WL(LL_INFO ,"LoadBroadcastChatMsgCSV:  %d,  %d  \n", m_woman_chat_msgs_[5].size(), m_man_chat_msgs_[5].size());
	//WL(LL_INFO ,"LoadBroadcastChatMsgCSV:  %d,  %d  \n", m_woman_chat_msgs_[6].size(), m_man_chat_msgs_[6].size());
	return true;
}


bool CsvDataMgr::LoadPrivateChipCsv()
{
	CCsvConfigReader tmpFileReader;
	if (!tmpFileReader.OpenFile(g_private_chips_csvname, false))
		return false;

	int i = 1;
	for (tmpFileReader.First(i); !tmpFileReader.IsDone(); ++i, tmpFileReader.Next())	{

		int objid = tmpFileReader.GetFieldValueToInt();
		if (objid <= 0)
			continue;

		csv_private_chips_t * tmpobj = new csv_private_chips_t();
		if (tmpobj == NULL)
			continue;

		tmpobj->small_chip = objid;
		tmpobj->max_chip = tmpFileReader.GetFieldValueToInt();		
		tmpobj->chap1 = tmpFileReader.GetFieldValueToInt();
		tmpobj->chap2 = tmpFileReader.GetFieldValueToInt();
		tmpobj->chap3 = tmpFileReader.GetFieldValueToInt();
		tmpobj->chap4 = tmpFileReader.GetFieldValueToInt();
		tmpobj->chap5 = tmpFileReader.GetFieldValueToInt();

		CSV_ALL_CHIPS_MAP::iterator it1 = m_private_chips_.find(tmpobj->small_chip);
		if (it1 == m_private_chips_.end()) {
			CSV_PRIVATE_MAP tmpmap;
			m_private_chips_.insert( CSV_ALL_CHIPS_MAP::value_type(tmpobj->small_chip, tmpmap) );
			it1 = m_private_chips_.find(tmpobj->small_chip);
			if (it1 == m_private_chips_.end())
				return false;
		}
			
		it1->second.insert( CSV_PRIVATE_MAP::value_type(tmpobj->max_chip, tmpobj) );
	}

	return true;
}

void CsvDataMgr::ReleasePrivateChipCsv()
{
	CSV_ALL_CHIPS_MAP::iterator it1 = m_private_chips_.begin();
	for (; it1 != m_private_chips_.end(); ++it1) {
		if (it1 == m_private_chips_.end())
			continue;

		CSV_PRIVATE_MAP::iterator it2 = it1->second.begin();
		for (; it2 != it1->second.end(); ++it2) {
			if (it2->second == NULL)
				continue;

			delete it2->second;
			it2->second = NULL;
		}

		it1->second.clear();
	}

	m_private_chips_.clear();
}

bool CsvDataMgr::LoadTigerCsv()
{
	CCsvConfigReader tmpFileReader;
	if (!tmpFileReader.OpenFile(g_tiger_random_csvname, false))
		return false;

	int i = 1;
	for (tmpFileReader.First(i); !tmpFileReader.IsDone(); ++i, tmpFileReader.Next())	{

		int objid = tmpFileReader.GetFieldValueToInt();
	
		int probability1 = tmpFileReader.GetFieldValueToInt();		
		int probability2 = tmpFileReader.GetFieldValueToInt();	
		int probability3 = tmpFileReader.GetFieldValueToInt();	
		int probability4 = tmpFileReader.GetFieldValueToInt();	
		int probability5 = tmpFileReader.GetFieldValueToInt();	
		int probability6 = tmpFileReader.GetFieldValueToInt();	
		int probability7 = tmpFileReader.GetFieldValueToInt();	
		int probability8 = tmpFileReader.GetFieldValueToInt();	

		Alias * pAliasObj = NULL;

		if (objid == 1) {
			m_tiger_one_start_ = tmpFileReader.GetFieldValueToInt();	
			m_tiger_one_end_ = tmpFileReader.GetFieldValueToInt();
			pAliasObj = &m_alias_one_;
		}
		else if (objid == 2) {
			m_tiger_two_start_ = tmpFileReader.GetFieldValueToInt();	
			m_tiger_two_end_ = tmpFileReader.GetFieldValueToInt();	
			pAliasObj = &m_alias_two_;
		}
		else if (objid == 3) {
			m_tiger_three_start_ = tmpFileReader.GetFieldValueToInt();	
			m_tiger_three_end_ = 999999999;	
			pAliasObj = &m_alias_three_;
		}
		else {
			continue;
		}
		
		int all_probability = probability1+probability2+probability3+probability4+probability5+probability6+probability7+probability8;

		//WL(LL_INFO, "LoadTigerCsv: all_probability = %d  \n", all_probability);

		pAliasObj->Clear();
		pAliasObj->Init();
		pAliasObj->AddElement(CT_WANG_PAI,	(float)probability1/(float)all_probability );
		pAliasObj->AddElement(CT_BAO_ZI,	(float)probability2/(float)all_probability );
		pAliasObj->AddElement(CT_SHUN_JIN,	(float)probability3/(float)all_probability );
		pAliasObj->AddElement(CT_DI_LONG,	(float)probability4/(float)all_probability );
		pAliasObj->AddElement(CT_SHUN_ZI,	(float)probability5/(float)all_probability );
		pAliasObj->AddElement(CT_JIN_HUA,	(float)probability6/(float)all_probability );
		pAliasObj->AddElement(CT_DOUBLE,	(float)probability7/(float)all_probability );
		pAliasObj->AddElement(CT_SINGLE,	(float)probability8/(float)all_probability );
		pAliasObj->InitTable();
		//pAliasObj->Print();
	}

	return true;
}


csv_store_t * CsvDataMgr::GetStoreInfo(int index)
{
	CSV_STORE_MAP::iterator it = m_stores_.find(index);
	if (it == m_stores_.end())
		return NULL;

	return it->second;
}


csv_private_chips_t * CsvDataMgr::GetPrivateChips(int small_bet, int max_bet)
{
	CSV_ALL_CHIPS_MAP::iterator it1 = m_private_chips_.find(small_bet);
	if (it1 == m_private_chips_.end())
		return NULL;

	CSV_PRIVATE_MAP::iterator it2 = it1->second.find(max_bet);
	if (it2 == it1->second.end())
		return NULL;

	return it2->second;
}

int	 CsvDataMgr::GetTigerRandomType(int curSorce)
{
	if (curSorce >= m_tiger_one_start_ && curSorce <= m_tiger_one_end_) {
		DWORD rand_num = 0;
		m_alias_one_.Rand(rand_num);
		return (int)rand_num;
	}
	else if (curSorce >= m_tiger_two_start_ && curSorce <= m_tiger_two_end_) {
		DWORD rand_num = 0;
		m_alias_two_.Rand(rand_num);
		return (int)rand_num;
	}
	else if (curSorce >= m_tiger_three_start_ && curSorce <= m_tiger_three_end_) {
		DWORD rand_num = 0;
		m_alias_three_.Rand(rand_num);
		return (int)rand_num;
	}
	else {
		DWORD rand_num = 0;
		m_alias_one_.Rand(rand_num);
		return (int)rand_num;
	}
}


std::string CsvDataMgr::GetRandomWomanName()
{
	int vecsize = m_woman_names_.size();
	if (vecsize <= 0)
		return "";

	int index = rand()%vecsize;
	return m_woman_names_[index];
}

std::string CsvDataMgr::GetRandomManName()
{
	int vecsize = m_man_names_.size();
	if (vecsize <= 0)
		return "";

	int index = rand()%vecsize;
	return m_man_names_[index];
}

std::string CsvDataMgr::GetRandomWomanChatMsg(int weekday)
{
	if (weekday < 0 || weekday >= 7)
		return "";

	int vecsize = m_woman_chat_msgs_[weekday].size();
	if (vecsize <= 0)
		return "";

	int index = rand()%vecsize;
	return m_woman_chat_msgs_[weekday][index];
}

std::string CsvDataMgr::GetRandomManChatMsg(int weekday)
{
	if (weekday < 0 || weekday >= 7)
		return "";

	int vecsize = m_man_chat_msgs_[weekday].size();
	if (vecsize <= 0)
		return "";

	int index = rand()%vecsize;
	return m_man_chat_msgs_[weekday][index];
}

int CsvDataMgr::gb2312toutf8(char *sourcebuf,size_t sourcelen,char *destbuf,size_t destlen) 
{   
	iconv_t cd;   
	
	// WCHAR_T
	if( (cd = iconv_open("utf-8","gb2312")) ==0 )     
		return -1;   
	
	memset(destbuf,0,destlen);   

	char **source = &sourcebuf;   

	char **dest = &destbuf;
	if(-1 == iconv(cd,source, &sourcelen, dest, &destlen))     
		return -1;   

	iconv_close(cd);   

	return 0;   

}


